package com.zyy.handler;

import com.zyy.service.JSONSerializer;
import com.zyy.service.RpcDecoder;
import com.zyy.service.RpcRequest;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author zyy
 * @date 2021/2/23 下午1:25
 * @description
 */
public class UserServiceInitializer extends ChannelInitializer<NioSocketChannel> {
    @Override
    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
        //设置管道对象
        ChannelPipeline pipeline = nioSocketChannel.pipeline();
        //设置编码
        pipeline.addLast(new StringEncoder()); //服务方返回的是一个字符串，要编码
        pipeline.addLast(new RpcDecoder(RpcRequest.class,new JSONSerializer()));
        //把自定义的channel Handler添加到通道中
        pipeline.addLast(new UserServiceHandler());
    }
}
